<?php

namespace App\Http\Controllers\Platform\Setting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DataResource;
use App\Models\Game\Platform\Org\Config;
use App\Models\Game\Config\CurrencyCustom;
use App\Models\Game\Config\Currency;

class ConfigController extends Controller
{
    /**
     * 清除config
     * @param Request $request
     */
    public function cleanData(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $updata['config'] = json_encode(new \stdClass());

        $update = Config::where('plat_code', $platCode)->update($updata);

        return new DataResource([$update]);
    }

    /**
     * 获取数据
     */
    public function getData(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $config = Config::where('plat_code', $platCode)->firstOrFail();

        return new DataResource($config);
    }

    /**
     * 更新或创建数据
     *
     * @param string $platCode
     */
    public function updateOrCreate(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64',
            'currency' => 'required|array|between:1,100',
            'payment' => 'required|integer|in:1,2,3',
            'rate' => 'nullable|numeric|between:0.0001,1',
            'max_award' => 'nullable|numeric',
            'game_validity_period' => 'nullable|numeric',
        ]);
        if (empty($request['game_validity_period'])){
            $request['game_validity_period'] = 0;
        }

        $config = Config::updateOrCreate($request->only([
            'plat_code'
        ]), $request->only([
            'payment', 'currency', 'rate', 'max_award','game_validity_period'
        ]));

        // 通知游戏端配置修改
        $config->syncGameService();

        return new DataResource($config);
    }


    /**
     * 更新或创建数据（config字段）
     * @param string $platCode
     */
    public function updateOrCreateConfig(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64',
        ]);

        $config = Config::updateOrCreate($request->only([
            'plat_code'
        ]), $request->only([
            'config'
        ]));

        // 通知游戏端配置修改
        $config->syncGameService();

        return new DataResource($config);
    }

    /**
     * 更新或创建数据（开关）
     * @param Request $request
     */
    public function updateOrCreateRtp(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64',
            'rtp_control_personal' => 'required|integer|in:1,2',
            'rtp_control_new' => 'required|integer|in:1,2',
        ]);

        $config = Config::updateOrCreate($request->only([
            'plat_code'
        ]), $request->only([
            'rtp_control_personal','rtp_control_new'
        ]));

        // 通知游戏端配置修改
        $config->syncGameService();

        return new DataResource($config);
    }

    /**
     * 添加自定义币种
     */
    public function customCurrency(Request $request)
    {
        $data = $request->validate([
            'real_currency' => 'required|string|between:1,6',
            'multiple' => 'required|numeric|between:0.00000001,10000000',
        ]);

        // 检查系统币种是否存在
        Currency::where('currency', $data['real_currency'])->firstOrFail();

        // 创建自定义币种
        $currency = CurrencyCustom::firstOrCreate([
            'currency' => $data['real_currency'] . 'x' . $data['multiple']
        ], $data);

        // 通知游戏端配置修改
        $currency->syncGameService();

        return new DataResource($currency);
    }

    /**
     * 更新或创建数据【UI】
     */
    public function updateOrCreateByUi(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64',
            'return_type' => 'required|integer|in:0,1,2',
            'return_url' => 'nullable|required_if:return_type,2|string|max:1024',
            'oem' => 'nullable|string|max:64',
            'more_game' => 'required|integer|in:1,2',
        ]);

        // 排除空字段
        $ui = collect($request->only([
            'return_type', 'return_url', 'oem', 'more_game'
        ]))->reject(function ($value) {
            return is_null($value);
        });

        $config = Config::updateOrCreate($request->only([
            'plat_code'
        ]), [
            'ui' => $ui->isEmpty() ? null : $ui->all()
        ]);

        // 通知游戏端配置修改
        $config->syncGameService();

        return new DataResource($config->ui);
    }

    /**
     * 更新或创建数据【接入游戏】
     */
    public function updateOrCreateByGames(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64',
            'games' => 'nullable|array',
        ]);

        $games = $request->input('games');

        $config = Config::updateOrCreate($request->only([
            'plat_code'
        ]), [
            'games' => empty($games) ? null : $games
        ]);

        // 通知游戏端配置修改
        $config->syncGameService();

        return new DataResource($config->games);
    }
}
